package com.lili.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.*;
import com.lili.mapper.*;
import com.lili.po.*;
import com.lili.uitl.ShiroUtils;
import com.lili.vo.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {
    @Autowired
    XasUserOrderGoodsMapper xasUserOrderGoodsMapper;
    @Autowired
    XasOrderGoodsListMapper xasOrderGoodsListMapper;
    @Autowired
    XasOrderBillListMapper xasOrderBillListMapper;
    @Autowired
    XasUserOrderRechargeMapper xasUserOrderRechargeMapper;
    @Autowired
    XasUserOrderWithdrawMapper xasUserOrderWithdrawMapper;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    XasUserInfoMapper xasUserInfoMapper;
    @Autowired
    UserService userService;


    public IPage<XasUserOrderGoods> selectXasUserOrderGoods(XasUserOrderGoodsRequest xasUserOrderGoodsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserOrderGoodsRequest, page);
        IPage<XasUserOrderGoods> iPage = xasUserOrderGoodsMapper.selectXasUserOrderGoods(page,
                xasUserOrderGoodsRequest.getOrderId(), xasUserOrderGoodsRequest.getPhone(), xasUserOrderGoodsRequest.getStartTime(), xasUserOrderGoodsRequest.getEndTime(),xasUserOrderGoodsRequest.getStatus());
        return iPage;
    }


    public List<XasOrderBillList> selectXasOrderBillList(XasOrderBillListRequest xasOrderBillListRequest) {
        LambdaQueryWrapper<XasOrderBillList> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasOrderBillList::getOrderId, xasOrderBillListRequest.getOrderId());
        List<XasOrderBillList> xasOrderBillLists = xasOrderBillListMapper.selectList(lambdaQueryWrapper);
        return xasOrderBillLists;
    }

    public XasOrderBillList selectXasOrderBill(XasOrderBillListRequest xasOrderBillListRequest) {
        LambdaQueryWrapper<XasOrderBillList> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasOrderBillList::getOrderId, xasOrderBillListRequest.getOrderId());
        XasOrderBillList xasOrderBillList = xasOrderBillListMapper.selectOne(lambdaQueryWrapper);
        return xasOrderBillList;
    }

    @Transactional
    public void updateXasOrderBillList(XasOrderBillList xasOrderBillList) {
        xasOrderBillListMapper.updateById(xasOrderBillList);
    }

    @Transactional
    public void addXasOrderBillList(XasOrderBillList xasOrderBillList) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasOrderBillList.setCreateUser(userLoginName);
        xasOrderBillList.setCreateTime(new Date());
        xasOrderBillListMapper.insert(xasOrderBillList);
    }


    public IPage<XasOrderGoodsList> selectXasOrderGoodsList(XasOrderGoodsListRequest xasUserOrderGoodsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserOrderGoodsRequest, page);
        IPage<XasOrderGoodsList> iPage = xasOrderGoodsListMapper.selectXasUserOrderGoods(page, xasUserOrderGoodsRequest.getOrderId());
        return iPage;
    }

    //    public void updateUserOrderList(XasUserOrderGoods xasUserOrderGoods) {
//        xasUserOrderGoodsMapper.updateById(xasUserOrderGoods);
//    }
    public void updateUserOrderList(XasOrderGoodsList xasOrderGoodsList) {
        xasOrderGoodsListMapper.updateById(xasOrderGoodsList);
        XasUserOrderGoods xasUserOrderGoods = xasUserOrderGoodsMapper.selectById(xasOrderGoodsList.getOrderId());
        if (xasUserOrderGoods != null && xasUserOrderGoods.getSendTime() == null) {
            XasUserOrderGoods update  = new XasUserOrderGoods();
            update.setId(xasUserOrderGoods.getId());
            update.setStatus(3);
            update.setSendTime(new Date());
            xasUserOrderGoodsMapper.updateById(update);
        }
    }


    public IPage<XasUserOrderRecharge> selectXasUserOrderRecharge(XasUserOrderRechargeRequest xasUserOrderRechargeRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserOrderRechargeRequest, page);
        IPage<XasUserOrderRecharge> iPage = xasUserOrderRechargeMapper.selectXasUserOrderRecharge(page, xasUserOrderRechargeRequest.getPhone(),
                xasUserOrderRechargeRequest.getStartTime(), xasUserOrderRechargeRequest.getEndTime());
        return iPage;
    }


    public IPage<XasUserOrderWithdraw> selectXasUserOrderWithdraw(XasUserOrderWithdrawRequest xasUserOrderRechargeRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserOrderRechargeRequest, page);
        IPage<XasUserOrderWithdraw> iPage = xasUserOrderWithdrawMapper.selectXasUserOrderWithdraw(page, xasUserOrderRechargeRequest.getPhone(), xasUserOrderRechargeRequest.getStatus(),
                xasUserOrderRechargeRequest.getStartTime(), xasUserOrderRechargeRequest.getEndTime());
        return iPage;
    }


    public boolean examineXasUserOrderWithdraw(String id) {
        RestRequest restRequest = new RestRequest();
        restRequest.setOrderId(id);
        restRequest.setStatus(2);
        ResponseEntity<Object> stringResponseEntity = restTemplate.postForEntity("http://47.108.49.148/xas/order/withdraw", restRequest, Object.class);
        Object body = stringResponseEntity.getBody();
        Object code = ((Map) body).get("code");
        if (StringUtils.equalsIgnoreCase("SUC", code.toString())) {
            return true;
        }
        return false;
    }

    public boolean examineRejectXasUserOrderWithdraw(String id) {
        RestRequest restRequest = new RestRequest();
        restRequest.setOrderId(id);
        restRequest.setStatus(6);
        ResponseEntity<Object> stringResponseEntity = restTemplate.postForEntity("http://47.108.49.148/xas/order/withdraw", restRequest, Object.class);
        Object body = stringResponseEntity.getBody();
        Object code = ((Map) body).get("code");
        if (StringUtils.equalsIgnoreCase(code.toString(), "SUC")) {
            return true;
        }
        return false;
    }

    public List<Map<String, Double>> countOrder(Date startTime, Date endTime) {
        List<Map<String, Double>> userInfo = xasUserInfoMapper.countOrder(startTime, endTime);
        return userInfo;
    }

    public List<Map<String, Double>> countTotalOrder() {
        List<Map<String, Double>> userInfo = xasUserInfoMapper.countTotalOrder();
        return userInfo;
    }
}

